package cupk;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class ProxySearcher extends Searcher{
    private RealSearcher searcher;
    private AccessValidator validator;
    private Logger logger;
    public boolean validate(String userId){
        validator = new AccessValidator();
        return validator.validate(userId);
    }
    public void log(String userId){
        logger = new Logger();
        logger.log(userId);
    }
    @Override
    public String doSearch(String userId, String keyWord) {
        String result = "";
        if(validate(userId)){
            log(userId);
            searcher = new RealSearcher();
            result = searcher.doSearch(userId,keyWord);
        } else {
            result =  "用户不合法！";
            try {
                FileWriter fileWriter = new FileWriter("log.txt", true);
                PrintWriter printWriter = new PrintWriter(fileWriter);
                printWriter.println("阻止了不合法用户" + userId + "的查询");
                printWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
